package org.pillar.fitness.web.controller.tienchin.clue;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.pillar.fitness.activity.service.IActivityService;
import org.pillar.fitness.channel.domain.Channel;
import org.pillar.fitness.channel.service.IChannelService;
import org.pillar.fitness.clue.domain.Clue;
import org.pillar.fitness.clue.domain.vo.ClueSummary;
import org.pillar.fitness.clue.service.IClueService;
import org.pillar.fitness.common.annotation.Log;
import org.pillar.fitness.common.core.controller.BaseController;
import org.pillar.fitness.common.core.domain.AjaxResult;
import org.pillar.fitness.common.core.page.TableDataInfo;
import org.pillar.fitness.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2023-01-16
 */
@RestController
@RequestMapping("/fitness/clue")
public class ClueController extends BaseController {

    @Autowired
    IClueService clueService;

    @Autowired
    IChannelService channelService;

    @Autowired
    IActivityService activityService;


    /**
     * 新增线索
     */
    @PreAuthorize("hasPermission('fitness:channel:create')")
    @Log(title = "渠道管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Clue clue) {
        return clueService.addClue(clue);
    }


    /**
     * 添加线索时 获取渠道的下拉框数据
     *
     * @return
     */
    @PreAuthorize("hasPermission('fitness:channel:create')")
    @GetMapping("/channels")
    public AjaxResult getAllChannels() {
        // 直接通过myplus方法返回所有渠道数据
        // 这里有一个问题就是如果渠道被禁用、渠道逻辑删除的也会被获取 应该写个查询排除一下
        QueryWrapper<Channel> qw = new QueryWrapper();
        qw.lambda().eq(Channel::getStatus, 1).eq(Channel::getDelFlag, 0);
        return AjaxResult.success(channelService.list(qw));
    }

    /**
     * 跟进渠道选中的channelId 获取对应的渠道活动信息
     * @param channelId 渠道id
     * @return
     */
    @PreAuthorize("hasPermission('fitness:channel:create')")
    @GetMapping("/activity/{channelId}")
    public AjaxResult getActivityByChannelId(@PathVariable Integer channelId) {
        return activityService.selectActivityByChannelId(channelId);
    }


    /**
     * 获取线索列表
     */
    @PreAuthorize("hasPermission('fitness:clue:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        //前端传入的搜索参数
        //System.out.println("channelVO=" + channelVO);
        //父类的参数
        //System.out.println("channelVO.getParams()=" + channelVO.getParams());
        startPage();
        List<ClueSummary> list = clueService.selectClueList();
        return getDataTable(list);
    }
}
